package cn.com.hfuu.control;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.hfuu.SystemUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.module.code;
import cn.com.hfuu.module.menu;
import cn.com.hfuu.module.role;
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.MenuService;
import cn.com.hfuu.service.RoleMenuService;
import cn.com.hfuu.vo.Page;

@Controller
@RequestMapping("menu")
public class MenuControl {

	@Autowired
	MenuService menuservice;
	
	@Autowired
	CodeService codeservice;
	
	@Autowired
	private RoleMenuService rolemenuservice;
	
	@RequestMapping("info")
	public String  init(Model m){
		List<menu> menulist=menuservice.selectfiirsttotal("0");//一级菜单
		for (menu menu : menulist) {
			menu.setSecond(menuservice.selectfiirsttotal(menu.getMenuId()));
		}
	
		m.addAttribute("menulist", menulist);
		return "menu/menu";
	}
	

	@RequestMapping("add")
	public String add(Model m){
		List<code> codelist=codeservice.selectbyType("10");
		List<menu> menulist=menuservice.selectfiirsttotal("0");
		m.addAttribute("codelist",codelist);
		m.addAttribute("menulist", menulist);
		return "menu/add";
	}
	
	@RequestMapping("addproc")
	@ResponseBody
	public String addproc(menu m){
		UUIdUtil id=new UUIdUtil();
		SystemUtil sys=new SystemUtil();
		m.setMenuId(id.getUuid());
		m.setMenuCreatetime(sys.getDatetime());
		System.out.println(m.toString());
		if(menuservice.insert(m)>0){
			return "success";
		}
		return "false";
	}
	
	@RequestMapping("edit")
	public String edit(String id,Model m){
		menu menu=menuservice.selectabyid(id);
		List<code> codelist=codeservice.selectbyType("10");
		List<menu> menulist=menuservice.selectfiirsttotal("0");
		m.addAttribute("codelist",codelist);
		m.addAttribute("menulist", menulist);
		m.addAttribute("menu", menu);
		return "menu/edit";
	}
	
	@RequestMapping("editproc")
	@ResponseBody
	public String editproc(menu m){
		if(menuservice.update(m)>0){
			return "success";
		}else{
			return "false";
		}
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String id){
		System.out.println(id);
		if(menuservice.delete(id)>0){
			rolemenuservice.deletemenuid(id);
			return "success";
		}else{
			return "false";
		}
	}
	
}
